<?php
class FavoriteController extends DefaultModuleController {
	public function init() {
		parent::init();
		if(!$this->userId) {
			$this->message('请您登记录后再操作。', GODHOUSE_DOMAIN_WWW."site-login.htm");
		}
	}
	
	public function actionAdd() {
		$productId = getgpc(2);
		$product = $this->ProductModel->findByPk($productId);
		if(!$product) {
			$this->message('操作失败，产品不存在。');
		}
		
		$favorite = $this->FavoriteModel->find("productId='$productId'");
		if($favorite) {
			$this->message('操作失败，您收藏产品已经被收藏。');
		}

		$att = array(
			'productId'=>$productId,
			'userId'=>$this->userId,
			'createdTime'=>$this->time,
		);
		$this->FavoriteModel->insert($att);
		$this->message('收藏成功。');
	}
	
	public function actionDelete() {
		$favoriteId = getgpc(2);
		if($favoriteId) {
			$this->FavoriteModel->deleteByPk($favoriteId);	
			$this->message('操作成功。', GODHOUSE_DOMAIN_WWW."user-favorite.htm");
		} else {
			$favoriteIdList = getgpc('favoriteId', 'P');
			//print_r($favoriteIdList);exit;
			foreach($favoriteIdList as $favoriteId) {
				$this->FavoriteModel->deleteByPk($favoriteId);	
			}
			$this->message('删除成功。', GODHOUSE_DOMAIN_WWW."user-favorite.htm");
		}
	}
}